﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
namespace NextCMS.Models
{
    /// <summary>
    /// Summary description for PageFormModel
    /// </summary>
    /// 
    public class PageFormModel
    {
        public int Id { get; set; }
        public int LanguageId { get; set; }
    }
    public class PageContactUsFormModel : IForm
    {
        public int Id { get; set; }
        public string SendEmailTo { get; set; }
        public string RedirectToURL { get; set; }
        public XElement SaveElement(IForm page)
        {
            PageContactUsFormModel pc = page as PageContactUsFormModel;
            var oMember = new XElement("PageForm",
             new XElement("Id", pc.Id),
             new XElement("SendEmailTo", pc.SendEmailTo),
             new XElement("RedirectToURL", pc.RedirectToURL)
             );
            return oMember;
        }


        // Edit Record
        public XElement EditElement(IForm page, XElement node)
        {
            PageContactUsFormModel pc = page as PageContactUsFormModel;
            node.SetElementValue("SendEmailTo", pc.SendEmailTo);
            node.SetElementValue("RedirectToURL", pc.RedirectToURL);

            return node;
        }




        public IForm GetById(XElement node)
        {
            if (node == null)
                return null;
            PageContactUsFormModel contactForm = new PageContactUsFormModel();
            contactForm.Id = (int)node.Element("Id");
            contactForm.RedirectToURL = node.Element("RedirectToURL").Value;
            contactForm.SendEmailTo = node.Element("SendEmailTo").Value;
            return contactForm;
        }


        public List<PageContactUsFormModel> GetAll(IEnumerable<XElement> nodeList)
        {
            var q = from node in nodeList
                    select new PageContactUsFormModel
                    {
                        Id = (int)node.Element("Id"),
                        RedirectToURL = node.Element("RedirectToURL").Value,
                        SendEmailTo = node.Element("SendEmailTo").Value
                    };
            return q.ToList();
        }

    }
    public interface IForm
    {
        int Id { get; set; }
        XElement SaveElement(IForm page);
        XElement EditElement(IForm page, XElement node);
    }
    public interface IPageForm : IDisposable
    {
        T GetById<T>(int id);
        List<T> GetAll<T>();
        int Save(IForm page);
        void Delete(int id);
        void Edit(IForm page);
    }
}